<template>
  <a-drawer title="下发明细" placement="right" width="60%" :visible="visible" @close="()=>{this.visible=false}">
    <a-table ref="table" size="default" rowKey="Id" :columns="columns" :data-source="assignDetail" :rowSelection="rowSelection" :pagination="false">
      <template slot="AssignType" slot-scope="text">
        <EnumName code="AssignType" :value="text"></EnumName>
      </template>
      <template slot="AssignValue" slot-scope="text,record">
        <span v-if="record.Assign.AssignType==='Diameter'">{{ record.Assign.Spec.Diameter }}</span>
        <span v-else-if="record.Assign.AssignType==='Width'">{{ record.Assign.Spec.Width }}</span>
        <span v-else-if="record.Assign.AssignType==='Height'">{{ record.Assign.Spec.Height }}</span>
        <span v-else-if="record.Assign.AssignType==='ToFrockHeight'">{{ record.Assign.Spec.ToFrockHeight }}</span>
        <span v-else-if="record.Assign.AssignType==='SideLength'">{{ record.Assign.Spec.SideLength }}</span>
        <span v-else-if="record.Assign.AssignType==='Name'">{{ record.Assign.Spec.Name }}</span>
        <span v-else-if="record.Assign.AssignType==='Code'">{{ record.Assign.Spec.Code }}</span>
      </template>
    </a-table>
    <div :style="{ position: 'absolute', bottom: 0, right: 0, width: '100%', borderTop: '1px solid #e9e9e9', padding: '10px 16px', background: '#fff', textAlign: 'right', zIndex: 1, }">
      <a-button :style="{ marginRight: '8px' }" type="primary" @click="handleAssign">下发</a-button>
      <a-button :style="{ marginRight: '8px' }" @click="()=>{this.visible=false}">关闭</a-button>
    </div>
  </a-drawer>
</template>

<script>
import moment from 'moment'
import MainSvc from '@/api/Define/MaterialBomSvc'
import DetailSvc from '@/api/Define/SpecAssignDetailSvc'
import EnumSelect from '@/components/CF/EnumSelect'
import EnumName from '@/components/CF/EnumName'

const columns = [
  { title: '规格名称', dataIndex: 'Assign.Spec.Name', sorter: true },
  { title: '规格编号', dataIndex: 'Assign.Spec.Code', sorter: true },
  { title: 'MES编号', dataIndex: 'Assign.Spec.MesCode', sorter: true },
  { title: '下发类型', dataIndex: 'Assign.AssignType', sorter: true, scopedSlots: { customRender: 'AssignType' } },
  { title: 'PLC IP', dataIndex: 'PlcIp', sorter: true },
  { title: '逻辑地址', dataIndex: 'LogAddr' },
  { title: '下发值', dataIndex: '', scopedSlots: { customRender: 'AssignValue' } }
]

export default {
  components: {
    MainSvc,
    EnumSelect,
    EnumName
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数 // 这里的ParentId具体业务主表字段Id
      queryParam: { BomId: '', Keyword: '' },
      selectedRowKeys: [],
      selectedRows: [],
      entity: {},
      assignDetail: [],
      type: 'Spec'
    }
  },
  filters: {
  },
  created() { },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    moment,
    openForm(record, type) {
      this.entity = record
      this.type = type
      // 这里的ParentId具体业务主表字段Id
      this.queryParam.BomId = record.Id
      this.visible = true
      DetailSvc.GetByBom(this.queryParam.BomId, type).then(result => {
        this.assignDetail = result.Data
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm() {
      // 这里的ParentId具体业务主表字段Id
      this.queryParam = { BomId: this.entity.Id, Keyword: '' }
    },
    handleAssign() {
      var thisObj = this
      var id = this.entity.Id
      this.$confirm({
        title: '确认下发此数据?',
        onOk() {
          return new Promise((resolve, reject) => {
            MainSvc.Assign(id, thisObj.type).then(result => {
              resolve()
              if (result.Success) {
                thisObj.visible = false
                thisObj.$message.success('操作成功!')
              } else {
                thisObj.$message.error(result.Msg)
              }
            })
          })
        }
      })
    }
  }
}
</script>
